<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>ELF进销存</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <script src="common.js"></script>
    <link rel="stylesheet" title="default" href="./sdk/sdk.css" />
    <link rel="stylesheet" href="./sdk/helper.css" />
    <link rel="stylesheet" href="./sdk/iconfont.css" />
    <script src="./sdk/sdk.js"></script>
    <script src="./sdk/vue.js"></script>
    <script src="./sdk/history.js"></script>
    <style>
        html,
        body,
        .app-wrapper {
            position: relative;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
    <div id="root" class="app-wrapper"></div>
    <script>
        (function () {
            let amis = amisRequire("amis/embed");
            const match = amisRequire("path-to-regexp").match;

            // 如果想用 browserHistory 请切换下这处代码, 其他不用变
            // const history = History.createBrowserHistory();
            const history = History.createHashHistory();

            let username = window.userService.user.name;

            const app = {
                type: "app",
                brandName: "ELF进销存",
                logo: "./public/logo.png",
                header: {
                    type: "grid",
                    columns: [
                        {
                            body: [],
                            id: "u:c7ed86879335",
                        },
                        {
                            body: [
                                {
                                    type: "dropdown-button",
                                    label: username,
                                    buttons: [
                                        {
                                            type: "button",
                                            label: "修改密码",
                                            id: "u:3a2620990f42",
                                            onEvent: {
                                                click: {
                                                    actions: [
                                                        {
                                                            ignoreError: false,
                                                            actionType: "dialog",
                                                            dialog: {
                                                                type: "dialog",
                                                                title: "修改密码",
                                                                body: [
                                                                    {
                                                                        id: "u:26307e7c510d",
                                                                        debug: true,
                                                                        type: "form",
                                                                        title: "表单",
                                                                        mode: "flex",
                                                                        labelAlign: "top",
                                                                        dsType: "api",
                                                                        feat: "Insert",
                                                                        body: [
                                                                            {
                                                                                name: "oldPassword",
                                                                                label: "旧密码",
                                                                                row: 0,
                                                                                type: "input-password",
                                                                                id: "u:272f807ae9d0",
                                                                                showCounter: false,
                                                                                colSize: "1",
                                                                                required: true,
                                                                            },
                                                                            {
                                                                                name: "newPassword",
                                                                                label: "新密码",
                                                                                row: 1,
                                                                                type: "input-password",
                                                                                id: "u:45cd2847dd9d",
                                                                                showCounter: false,
                                                                                colSize: "1",
                                                                                required: true,
                                                                            },
                                                                            {
                                                                                name: "confirmNewPassword",
                                                                                label: "确认密码",
                                                                                row: 2,
                                                                                type: "input-password",
                                                                                id: "u:a4bd46269e6e",
                                                                                showCounter: false,
                                                                                validations: {
                                                                                    equalsField: "newPassword",
                                                                                },
                                                                                required: true,
                                                                                validationErrors: {
                                                                                    equalsField:
                                                                                        "两次输入的新密码不匹配",
                                                                                },
                                                                            },
                                                                        ],
                                                                        api: {
                                                                            url: "/api/identity/identity/identity/identity/identity/users/updatepassword",
                                                                            method: "put",
                                                                            messages: {
                                                                                success: "密码修改成功",
                                                                            },
                                                                        },
                                                                        actions: [
                                                                            {
                                                                                type: "button",
                                                                                label: "提交",
                                                                                onEvent: {
                                                                                    click: {
                                                                                        actions: [
                                                                                            {
                                                                                                actionType: "submit",
                                                                                                componentId: "u:26307e7c510d",
                                                                                            },
                                                                                        ],
                                                                                    },
                                                                                },
                                                                                level: "primary",
                                                                            },
                                                                        ],
                                                                        resetAfterSubmit: true,
                                                                    },
                                                                ],
                                                                id: "u:6158f36072fb",
                                                                actions: [
                                                                    {
                                                                        type: "button",
                                                                        actionType: "cancel",
                                                                        label: "取消",
                                                                        id: "u:a3d8e688f327",
                                                                    },
                                                                    {
                                                                        type: "button",
                                                                        actionType: "confirm",
                                                                        label: "确定",
                                                                        primary: true,
                                                                        id: "u:9894c9d856a7",
                                                                    },
                                                                ],
                                                                showCloseButton: true,
                                                                closeOnOutside: false,
                                                                closeOnEsc: false,
                                                                showErrorMsg: true,
                                                                showLoading: true,
                                                                draggable: false,
                                                                actionType: "dialog",
                                                            },
                                                        },
                                                    ],
                                                },
                                            },
                                            disabledOnAction: false,
                                        },
                                        {
                                            type: "button",
                                            label: "退出登录",
                                            id: "u:0312276061c2",
                                            onEvent: {
                                                click: {
                                                    actions: [
                                                        {
                                                            ignoreError: false,
                                                            script: "window.userService.logout();\n      ",
                                                            actionType: "custom",
                                                        },
                                                    ],
                                                },
                                            },
                                            disabledOnAction: false,
                                        },
                                    ],
                                    id: "u:d7d01012eace",
                                    size: "lg",
                                    block: false,
                                    level: "light",
                                    trigger: "hover",
                                },
                            ],
                            id: "u:e3a7dae18ccf",
                            md: "auto",
                        },
                    ],
                    id: "u:2b8735585ee8",
                },
                // footer: '<div class="p-2 text-center bg-light">底部区域</div>',
                // asideBefore: '<div class="p-2 text-center">菜单前面区域</div>',
                // asideAfter: '<div class="p-2 text-center">菜单后面区域</div>',
                api: "./pages/site.json",
            };

            function normalizeLink(to, location = history.location) {
                to = to || "";

                if (to && to[0] === "#") {
                    to = location.pathname + location.search + to;
                } else if (to && to[0] === "?") {
                    to = location.pathname + to;
                }

                const idx = to.indexOf("?");
                const idx2 = to.indexOf("#");
                let pathname = ~idx
                    ? to.substring(0, idx)
                    : ~idx2
                        ? to.substring(0, idx2)
                        : to;
                let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : "";
                let hash = ~idx2 ? to.substring(idx2) : location.hash;

                if (!pathname) {
                    pathname = location.pathname;
                } else if (pathname[0] != "/" && !/^https?\:\/\//.test(pathname)) {
                    let relativeBase = location.pathname;
                    const paths = relativeBase.split("/");
                    paths.pop();
                    let m;
                    while ((m = /^\.\.?\//.exec(pathname))) {
                        if (m[0] === "../") {
                            paths.pop();
                        }
                        pathname = pathname.substring(m[0].length);
                    }
                    pathname = paths.concat(pathname).join("/");
                }

                return pathname + search + hash;
            }

            function isCurrentUrl(to, ctx) {
                if (!to) {
                    return false;
                }
                const pathname = history.location.pathname;
                const link = normalizeLink(to, {
                    ...location,
                    pathname,
                    hash: "",
                });

                if (!~link.indexOf("http") && ~link.indexOf(":")) {
                    let strict = ctx && ctx.strict;
                    return match(link, {
                        decode: decodeURIComponent,
                        strict: typeof strict !== "undefined" ? strict : true,
                    })(pathname);
                }

                return decodeURI(pathname) === link;
            }

            let amisInstance = amis.embed(
                "#root",
                app,
                {
                    location: history.location,
                    data: {
                        // 全局数据，是受控的数据
                    },
                    context: {
                        // 全局上下文数据, 非受控的数据，无论哪一层都能获取到，包括弹窗自定义数据映射后都能获取到。
                        // 可以用来放一下全局配置等。比如 API_HOST, 这样页面配置里面可以通过 ${API_HOST} 来获取到。
                        API_HOST: "https://localhost:5001",
                        SELF_HOST: "http://localhost/AmisIdentity/#",
                    },
                },
                {
                    requestAdaptor: window.api.requestAdaptor,
                    responseAdaptor: window.api.responseAdaptor,
                    // // 全局 api 请求适配器
                    // // 另外在 amis 配置项中的 api 也可以配置适配器，针对某个特定接口单独处理。

                    // requestAdaptor(api) {
                    //   // 支持异步，可以通过 api.mockResponse 来设置返回结果，跳过真正的请求发送
                    //   // 此功能自定义 fetcher 的话会失效
                    //   // api.context 中包含发送请求前的上下文信息
                    //   return api;
                    // },

                    // // 全局 api 适配器。
                    // // 另外在 amis 配置项中的 api 也可以配置适配器，针对某个特定接口单独处理。
                    // responseAdaptor(api, payload, query, request, response) {
                    //   if (
                    //     payload.status != undefined &&
                    //     payload.msg != undefined &&
                    //     payload.data != undefined
                    //   ) {
                    //     return payload;
                    //   }
                    //   if (payload.error) {
                    //     var msg =
                    //       payload.error.details ||
                    //       payload.error.message ||
                    //       "未知错误！";
                    //     console.log(payload.error);
                    //     return {
                    //       status: -1,
                    //       msg: msg,
                    //       data: payload,
                    //     };
                    //   }

                    //   if (response.status >= 200 && response.status < 300) {
                    //     return {
                    //       status: 0,
                    //       msg: "",
                    //       data: payload,
                    //     };
                    //   } else {
                    //     return {
                    //       status: -1,
                    //       msg: response.status,
                    //       data: payload,
                    //     };
                    //   }

                    //   console.log(api, payload, query, request, response);
                    //   return payload;
                    // },

                    // watchRouteChange: fn => {
                    //   return history.listen(fn);
                    // },
                    updateLocation: (location, replace) => {
                        location = normalizeLink(location);
                        if (location === "goBack") {
                            return history.goBack();
                        } else if (
                            (!/^https?\:\/\//.test(location) &&
                                location ===
                                history.location.pathname + history.location.search) ||
                            location === history.location.href
                        ) {
                            // 目标地址和当前地址一样，不处理，免得重复刷新
                            return;
                        } else if (/^https?\:\/\//.test(location) || !history) {
                            return (window.location.href = location);
                        }

                        history[replace ? "replace" : "push"](location);
                    },
                    jumpTo: (to, action) => {
                        if (to === "goBack") {
                            return history.goBack();
                        }

                        to = normalizeLink(to);

                        if (isCurrentUrl(to)) {
                            return;
                        }

                        if (action && action.actionType === "url") {
                            action.blank === false
                                ? (window.location.href = to)
                                : window.open(to, "_blank");
                            return;
                        } else if (action && action.blank) {
                            window.open(to, "_blank");
                            return;
                        }

                        if (/^https?:\/\//.test(to)) {
                            window.location.href = to;
                        } else if (
                            (!/^https?\:\/\//.test(to) &&
                                to === history.pathname + history.location.search) ||
                            to === history.location.href
                        ) {
                            // do nothing
                        } else {
                            history.push(to);
                        }
                    },
                    isCurrentUrl: isCurrentUrl,
                    theme: "cxd",
                }
            );

            history.listen((state) => {
                amisInstance.updateProps({
                    location: state.location || state,
                });
            });
        })();
    </script>
</body>
</html>
